package com.ljdemo.lrecorder.utils;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

/**
 * 权限申请类
 * 必需实现 onRequestPermissionsResult 和 onActivityResult
 * 并调用相关Call方法
 *  <br/>
 *  date : 2023/6/9
 * */
public class PermissionUtil {

    private static final String TAG="权限请求工具";
    private AppCompatActivity activity;
    private String[] permissions;
    private int requestCode;

    /**
     * 默认请求码为 200
     * @param permissions 权限数组 Manifest.permission中定义了权限
     * @param activity 申请权限的活动
     * */
    public PermissionUtil(String[] permissions, AppCompatActivity activity){
        this.permissions=permissions;
        this.activity=activity;
        this.requestCode=200;
    }

    public PermissionUtil(String[] permissions, AppCompatActivity activity, int requestCode){
        this.permissions=permissions;
        this.activity=activity;
        this.requestCode=requestCode;
    }

    public static PermissionUtil constructor(String[] permissions, AppCompatActivity activity){
        return new PermissionUtil(permissions,activity);
    }

    public static PermissionUtil constructor(String[] permissions, AppCompatActivity activity,int requestCode){
        return new PermissionUtil(permissions,activity,requestCode);
    }

    /**
     * 申请权限
     * 必需实现 onRequestPermissionsResult 和 onActivityResult
     * 并调用相关Call方法
     *
     * @return
     */
    public PermissionUtil checkPermission(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            for (String permission : permissions){
                if (ContextCompat.checkSelfPermission(activity,permission) != PackageManager.PERMISSION_GRANTED){
                    ActivityCompat.requestPermissions(activity,permissions,requestCode);
                }
            }
        }else {
            Log.e(TAG,"权限申请失败，SDK版本过低");
        }
        return this;
    }

    /**
     * onRequestPermissionsResult调用
     * */
    public void onRequestPermissionsResultCall(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
        // 判断SDK版本大于M，请求码是当前请求码
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && requestCode == this.requestCode) {
            // 遍历权限数组
            for (int i = 0; i < permissions.length; i++) {
                // 如果没有授权
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    // 跳转到系统当前应用的设置页，让用户手动授权
                    Intent intent = new Intent();
                    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
                    intent.setData(uri);
                    activity.startActivityForResult(intent, this.requestCode);
                    return;
                }
            }
        }
    }

    /**
     * onActivityResult调用
     * */
    public void onActivityResultCall(int requestCode, int resultCode, @Nullable Intent data){
        if (resultCode == activity.RESULT_OK && requestCode == this.requestCode) {
            this.checkPermission();
        }
    }

}
